import os

from django.db.models import Q
from rest_framework import serializers

from apps.club_system.club.models import Club, ClubMember, ClubQuit
from apps.common.account.models import Account


class ClubQuitSerializerUpdateSerializer(serializers.ModelSerializer):
    pass


class ClubQuitSerializer(serializers.ModelSerializer):
    created_time = serializers.DateTimeField(format="%Y-%m-%d %H:%M", required=False, read_only=True)
    updated_time = serializers.DateTimeField(format="%Y-%m-%d %H:%M", required=False, read_only=True)

    club = serializers.StringRelatedField(read_only=True)
    # account = serializers.SlugRelatedField(label='申请人', slug_field='username', read_only=True)
    club_id = serializers.IntegerField()
    account = serializers.HiddenField(  # 默认为当前创建者
        default=serializers.CurrentUserDefault()
    )
    # #测试用
    # account_id = serializers.IntegerField()
    # account = serializers.StringRelatedField(read_only=True)
    first_check_status = serializers.CharField(read_only=True)
    second_check_status = serializers.CharField(read_only=True)

    def update(self, instance, validated_data):
        # 每次修改都将两个审核改为待审核
        validated_data['first_check_status'] = '待审核'
        validated_data['second_check_status'] = '待审核'
        for attr, value in validated_data.items():
            setattr(instance, attr, value)
        instance.save()
        return instance

    def to_representation(self, instance):
        ret = super().to_representation(instance)
        # 返回个人信息
        ret['realname'] = instance.account.realname
        ret['username'] = instance.account.username
        ret['student_community'] = instance.account.student_community
        ret['politics_status'] = instance.account.politics_status
        ret['faculty_title'] = instance.account.faculty_title
        # 添加审核历史
        ret['check_history'] = []
        check_qs = instance.club_quit_check.order_by('-created_time').only(
            'account', 'check_opinion', 'check_status', 'check_level', 'created_time'
        )
        check_level_fomat = {1: '一级审核', 2: '二级审核'}
        for i in check_qs:
            tmp = {
                'account': i.account.realname,
                'check_opinion': i.check_opinion,
                'check_status': i.check_status,
                'check_level_name': check_level_fomat.get(i.check_level, '审核'),
                'created_time': i.created_time,
            }
            ret['check_history'].append(tmp)
        return ret

    class Meta:
        model = ClubQuit
        fields = '__all__'


# 社团退团一级审核序列化器
class ClubQuitFirstCheckSerializer(serializers.ModelSerializer):
    created_time = serializers.DateTimeField(format="%Y-%m-%d %H:%M", required=False, read_only=True)
    updated_time = serializers.DateTimeField(format="%Y-%m-%d %H:%M", required=False, read_only=True)

    def to_representation(self, instance):
        ret = super(ClubQuitFirstCheckSerializer, self).to_representation(instance)
        ret['club_name'] = instance.club.name  # 添加社团名字
        # 返回个人信息
        ret['realname'] = instance.account.realname
        ret['username'] = instance.account.username
        ret['student_community'] = instance.account.student_community
        ret['politics_status'] = instance.account.politics_status
        ret['faculty_title'] = instance.account.faculty_title
        return ret

    class Meta:
        model = ClubQuit
        fields = '__all__'


# 社团退团二级审核序列化器
class ClubQuitSecondCHeckSerializer(serializers.ModelSerializer):
    created_time = serializers.DateTimeField(format="%Y-%m-%d %H:%M", required=False, read_only=True)
    updated_time = serializers.DateTimeField(format="%Y-%m-%d %H:%M", required=False, read_only=True)

    def to_representation(self, instance):
        ret = super(ClubQuitSecondCHeckSerializer, self).to_representation(instance)
        ret['club_name'] = instance.club.name  # 添加社团名字
        # 返回个人信息
        ret['realname'] = instance.account.realname
        ret['username'] = instance.account.username
        ret['student_community'] = instance.account.student_community
        ret['politics_status'] = instance.account.politics_status
        ret['faculty_title'] = instance.account.faculty_title

        # 添加一级审核意见
        check_qs = instance.club_quit_check.filter(check_level=1).order_by('created_time').first()
        ret['first_check_opinion'] = check_qs.check_opinion if check_qs else ' '
        return ret

    class Meta:
        model = ClubQuit
        fields = '__all__'
